﻿using System;
using System.Data;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
namespace UniMana.Win.Leadership
{
    public partial class FrmReport_SubStudent_Status : DevExpress.XtraEditors.XtraForm
    {
        private const int ACTION_NO = 0;

        // DBMain _sys = new DBMain();
        DateTime d = SysDateTime.Now;

        private DBMain m_DBMain = new DBMain();
        DBMain m_data = new DBMain();
        private DBMain m_ReportStudentStatus = new DBMain();
        int m_iDepartmentKey = 0;
        int m_iMajorKey = 0;
        int m_iTrainingTypeKey = 0;
        int m_iCourseKey = 0;
        int m_iLevelKey = 0;
        DataTable dt = new DataTable();
        private int _Flag = 1;

        public FrmReport_SubStudent_Status()
        {
            InitializeComponent();
            LoadData();
            InitControlEvent();
        }

        public void InitControlEvent()
        {
            //  this.xucBar1.btnPrint +=
            this.xucBar1.btnPrint_Click += 
                new UniMana.Utils.Controls.xucBar.ButtonClickHandler(this.btnPrint_Click);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            string ngay = "Tân An, ngày " + d.Day + 
                " ,tháng " + d.Month + " ,năm " + d.Year;
            printgrcStudent_Status.RtfReportFooter = 
                this.printgrcStudent_Status.RtfReportFooter.Replace(
                "ngaythang",
                UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ngay));
            printgrcStudent_Status.ShowPreview();
        }
        public void LoadData()
        {
            try
            {
                dt = this.m_ReportStudentStatus.m_DCReadOnly.Catalogue_Courses.ToDataTable();
                this.lueCatalogue_Course.GetData(dt, "Catalogue_CourseKey", "Name", noChoose: true);
                this.lueCatalogue_Course.EditValue = -1;

                dt = (
                    this.m_DBMain.m_DCReadOnly.Catalogue_Departments.GetDepartmentInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Department.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    m_DBMain.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
                lueCatalogue_Major.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_TrainingType.GetData(dt, noChoose: true);
                lueCatalogue_TrainingType.EditValue = 2;

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Courses.GetCourseKeyNameInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Course.GetData(dt, noChoose: true);

                dt = 
                    (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(
                    this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms,
                    this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                lueCatalogue_Level.GetData(dt, noChoose: true);

            }
            catch { };
        }

        private int CheckInfo()
        {
            if ((lueCatalogue_Department.EditValue != null) && 
                (lueCatalogue_Major.EditValue != null) && 
                (lueCatalogue_TrainingType.EditValue != null) && 
                (lueCatalogue_Course.EditValue != null) && 
                (lueCatalogue_Level.EditValue != null))
            {
                m_iDepartmentKey = (int)lueCatalogue_Department.EditValue;
                m_iMajorKey = (int)lueCatalogue_Major.EditValue;
                m_iTrainingTypeKey = (int)lueCatalogue_TrainingType.EditValue;
                m_iCourseKey = (int)lueCatalogue_Course.EditValue;
                m_iLevelKey = (int)lueCatalogue_Level.EditValue;
                return 1;
            }
            else
            {
                return 0;
            }
        }

        private void bttView_Click(object sender, EventArgs e)
        {
            if (CheckInfo() == 1)
            {
                // try
                //{

                //them so thu tu vao luoi
                DataTable dt = 
                    m_ReportStudentStatus.m_DCReadOnly.Training_Student_Classes.Student_StatusData(
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_Courses, 
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_Departments, 
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_Levels, 
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_Majors, 
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_Status, 
                    m_ReportStudentStatus.m_DCReadOnly.Catalogue_TrainingTypes, 
                    lueCatalogue_Course.GetIntValue(), 
                    lueCatalogue_Department.GetIntValue(), 
                    lueCatalogue_Major.GetIntValue(), 
                    lueCatalogue_TrainingType.GetIntValue(), 
                    lueCatalogue_Level.GetIntValue()).ToDataTable();
                dt.DefaultView.Sort = "Catalogue_CourseKey";
                dt = dt.DefaultView.ToTable();
                dt.Columns.Add("STT");
                dt.Rows[0]["STT"] = 1;

                int dem = 2;
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Catalogue_CourseKey"].ToString() != 
                        dt.Rows[i - 1]["Catalogue_CourseKey"].ToString())
                    {
                        dem = 1;
                        dt.Rows[i]["STT"] = dem;
                        dem++;
                    }
                    else
                    {
                        dt.Rows[i]["STT"] = dem;
                        dem++;
                    }
                }
                this.grcStudent_Status.DataSource = dt;
                this.grvStudent_Status.ExpandAllGroups();

                //  }
                // catch { MessageBox.Show("Lỗi", "Thông báo"); }
            }
            else
            {
                this.grcStudent_Status.DataSource = null;
                MessageBox.Show("Chưa chọn đầy đủ thông tin", "Thông báo");
            }
        }

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue) > 0)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                    {
                        dt = (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorKeyByDepartmentLevel(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), 
                            GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue))).ToDataTable();
                        lueCatalogue_Major.EditValue = dt.Rows[0][0];
                    }
                    else
                    {
                        // no action
                    }
                }
                else
                {
                    // no action
                }
            }
            catch { }
        }

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (_Flag == 1)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) <= 0)
                    {
                        // Nạp lại dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học

                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        lueCatalogue_Level.GetData(dt, noChoose: true);

                        lueCatalogue_Major.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms,
                            m_ReportStudentStatus.m_DCReadOnly.Catalogue_Levels)).ToDataTable();
                        lueCatalogue_Major.GetData(dt, noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors)).ToDataTable();
                        lueCatalogue_TrainingType.GetData(dt, noChoose: true);

                    }
                    else
                    {
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa

                        lueCatalogue_Level.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Levels.GetLevelByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Level.GetData(dt, noChoose: true);

                        lueCatalogue_Major.EditValue = null;
                        dt = (
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetMajorByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms,
                            m_ReportStudentStatus.m_DCReadOnly.Catalogue_Levels, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Major.GetData(dt, noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_TrainingType.GetData(dt, noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        dt = 
                            (this.m_DBMain.m_DCReadOnly.Catalogue_Courses.GetCourseByDepartment(
                            this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                            this.m_DBMain.m_DCReadOnly.Catalogue_Majors, 
                            GetValueLookUpEdit.ConvertValueToInt(
                            lueCatalogue_Department.EditValue))).ToDataTable();
                        this.lueCatalogue_Course.GetData(dt, noChoose: true);
                    }
                }
            }
            catch { }
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Major.EditValue) > 0)
                {

                    dt = 
                        (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetDepartmentKeyByMajor(
                        this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Major.EditValue))).ToDataTable();
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = dt.Rows[0][0];
                    _Flag = 1;

                    dt = 
                        (this.m_DBMain.m_DCReadOnly.Catalogue_Majors.GetLevelKeyByDepartmentMajor(
                        this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms, 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Department.EditValue), 
                        GetValueLookUpEdit.ConvertValueToInt(
                        lueCatalogue_Major.EditValue))).ToDataTable();
                    lueCatalogue_Level.EditValue = dt.Rows[0][0];

                }
                else
                {

                }
            }
            catch { }
        }

    }
}

